<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增课程')" />
    <th:block th:include="include :: select2-css" />
</head>
<body>
<div class="main-content">
    <form id="form-course-add" class="form-horizontal">
        <input name="deptId" type="hidden" id="treeId"/>
        <h4 class="form-header h4">基本信息</h4>

        <div class="form-group" >
            <label class="col-xs-3 control-label">所属机构：</label>
            <div class="col-xs-8">
                <select id="institution" name="institution" class="form-control m-b">
                    <option value="" th:text="选择机构"></option>
                    <option th:each="institution:${institutions}" th:value="${institution.itId}" th:text="${institution.itName}" ></option>
                </select>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label">课程名称：</label>
            <div class="col-sm-8">
                <input id="cName" name="cName" placeholder="请输入课程名称" class="form-control" type="text" maxlength="30" required>
            </div>
        </div>

        <div class="form-group">
            <label class="col-xs-3 control-label">选择年级：</label>
            <div class="col-xs-8">
                <select id="grade" name="grade" class="form-control m-b">
                    <option value="" th:text="选择年级">
                    <option th:each="grade:${grades}" th:value="${grade.gId}" th:text="${grade.gName}" ></option>
                </select>
            </div>
        </div>

        <div class="form-group">
            <label class="col-xs-3 control-label">选择类型：</label>
            <div class="col-xs-8">
                <select id="type" name="type" class="form-control m-b">
                    <option value="" th:text="选择类型">
                    <option th:each="type:${coursetypes}" th:value="${type.tId}" th:text="${type.tName}" th:disabled="${type.tStatus == '1'}" ></option>
                </select>
            </div>
        </div>

        <div class="form-group">
            <label class="col-xs-3 control-label">课程信息：</label>
            <div class="col-xs-8">
                <textarea id="cRemark" name="cRemark" class="form-control"></textarea>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label">课程信息：</label>
            <div class="col-sm-8">
                <input type="hidden" id="itInfo" name="itInfo" class="form-control" value="">
                <div id="itInfoinsert">

                </div>
            </div>
        </div>

        <div class="form-group">
            <label class="col-xs-3 control-label">可报名人数：</label>
            <div class="col-xs-8">
                <input id="cAvailablenum" name="cAvailablenum" class="form-control" />
            </div>
        </div>

    </form>
</div>

<!--<div class="row">-->
<!--    <div class="col-sm-offset-5 col-sm-10">-->
<!--        <button type="button" class="btn btn-sm btn-primary" onclick="submitHandler()"><i class="fa fa-check"></i>保 存</button>&nbsp;-->
<!--        <button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"><i class="fa fa-reply-all"></i>关 闭 </button>-->
<!--    </div>-->
<!--</div>-->
<th:block th:include="include :: footer" />
<th:block th:include="include :: select2-js" />
<script type="text/javascript" src="/ruoyi/js/wangEditor.js"></script>
<script th:inline="javascript">

    var prefix = ctx + "itsystem/course";

    const E = window.wangEditor
    const editor = new E('#itInfoinsert')
    // 或者 const editor = new E( document.getElementById('div1') )

    // 配置 图片上传server 接口地址
    editor.config.uploadImgServer = ctx + "itsystem/course" + "/imgupload";
    editor.config.uploadFileName = "img";

    editor.config.excludeMenus = [
        'video',
        'backColor',
        'code'
    ]

    editor.create();

    // 配置 onchange 回调函数
    editor.config.onchange = function (newHtml) {
        $("#itInfo").val(newHtml); // 获取最新的 html 内容
    }
    // 配置触发 onchange 的时间频率，默认为 200ms
    editor.config.onchangeTimeout = 1; // 修改为 500ms

    $("#form-course-add").validate({
        onkeyup: false,
        rules:{
            cName:{
                minlength: 2,
                maxlength: 20,
                remote: {
                    url: prefix + "/",
                    type: "post",
                    dataType: "json",
                    data: {
                            "course":{
                            "cName": function() {
                                console.log($.common.trim($("#cName").val()));
                                return $.common.trim($("#cName").val());
                            },
                            "itId": function() {
                                console.log($.common.trim($("#institution").val()));
                                return $.common.trim($("#institution").val());
                            }
                        },
                    },
                    dataFilter: function(data, type) {
                        return $.validate.unique(data);
                    }
                }
            },
            cAvailablenum:{
                function() {
                    return isNaN($("#cAvailablenum").val());
                },
            }
        },
        messages: {
            "cAvailablenum": {
                remote: "录入数字"
            },
            "cName": {
                remote: "课程已存在"
            },
        },
        focusCleanup: true
    });

    function submitHandler() {
        var chrtype = [[${#strings.defaultString(@config.getKey('sys.account.chrtype'), 0)}]];

        if ($.validate.form() ) {
            var data = $("#form-course-add").serializeArray();
            var cName= $("#cName").val();
            var itId= $("#institution").val();
            var cTypeid = $("#type").val();
            var gId= $("#grade").val()
            var cRemark = $("#cRemark").val();
            console.log(cName);
            data.push({"name": "cName", "value": cName});
            data.push({"name": "itId", "value": itId});
            data.push({"name": "cTypeid", "value": cTypeid});
            data.push({"name": "cRemark", "value": cRemark});
            data.push({"name": "gId", "value": gId});
            $.operate.saveTab(prefix + "/add", data);
        }
    }
</script>
</body>
</html>